package by.bsu.scs.db.dao;

// Generated 05.03.2011 11:02:55 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import by.bsu.scs.model.Task;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.model.User;
import by.bsu.scs.util.db.HibernateUtil;

/**
 * Home object for domain model class TaskAttachment.
 * 
 * @see by.bsu.scs.model.TaskAttachment
 * @author Hibernate Tools
 */
public class TaskAttachmentHome {

	private static final Logger LOGGER = Logger
			.getLogger(TaskAttachmentHome.class);

	private static final String INSTRUCTOR_ID = "instructorId";
	private static final String STUDENT_ID = "studentId";
	private static final String TASK_ID = "taskId";
	private static final String STATE = "state";
	private static final String GET_TASK_ATTACHMENT_BY_INSTRUCTOR_ID_AND_STATE = "getTaskAttachmentByIdInstructorAndState";
	private static final String GET_TASK_ATTACHMENT_NAME_BY_TASK_ID_AND_STATE = "getTaskAttachmentNameByIdTaskAndState";
	private static final String GET_TASK_ATTACHMENT_BY_STUDENT_ID_AND_TASK_ID = "getTaskAttachmentByIdStudentAndIdTask";
	private static final String GET_TASK_ATTACHMENT_BY_STUDENT_ID_TASK_ID_AND_STATE = "getTaskAttachmentByIdStudentIdTaskAndState";

	private Session session = null;
	private Transaction transaction = null;

	public void persist(TaskAttachment transientInstance) {
		LOGGER.info("persisting TaskAttachment instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			LOGGER.info("persist successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("persist failed", e.getCause());
		}
	}

	public void attachDirty(TaskAttachment instance) {
		LOGGER.info("attaching dirty TaskAttachment instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.saveOrUpdate(instance);
			transaction.commit();
			LOGGER.info("attach successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("attach failed", e.getCause());
		}
	}

	@SuppressWarnings("deprecation")
	public void attachClean(TaskAttachment instance) {
		LOGGER.info("attaching clean TaskAttachment instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			transaction.commit();
			LOGGER.info("attach successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("attach failed", e.getCause());
		}
	}

	public void delete(TaskAttachment persistentInstance) {
		LOGGER.info("deleting TaskAttachment instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.delete(persistentInstance);
			transaction.commit();
			LOGGER.info("delete successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("delete failed", e.getCause());
		}
	}

	public TaskAttachment merge(TaskAttachment detachedInstance) {
		LOGGER.info("merging TaskAttachment instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			TaskAttachment result = (TaskAttachment) session
					.merge(detachedInstance);
			transaction.commit();
			LOGGER.info("merge successful");
			return result;
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("merge failed", e.getCause());
			return null;
		}
	}

	public TaskAttachment findById(int id) {
		LOGGER.info("getting TaskAttachment instance with id: " + id);
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			TaskAttachment instance = (TaskAttachment) session.get(
					"by.bsu.scs.model.TaskAttachment", id);
			if (instance == null) {
				LOGGER.info("get successful, no instance found");
			} else {
				LOGGER.info("get successful, instance found");
			}
			transaction.commit();
			return instance;
		} catch (RuntimeException e) {
			transaction.rollback();
			LOGGER.error("get failed", e.getCause());
			return null;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByExample(TaskAttachment instance) {
		LOGGER.info("finding TaskAttachment instance by example");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			Example example = Example.create(instance).excludeZeroes();
			List results = session
					.createCriteria("by.bsu.scs.model.TaskAttachment")
					.add(example).list();
			LOGGER.info("find by example successful, result size: "
					+ results.size());
			transaction.commit();
			return results;
		} catch (RuntimeException e) {
			transaction.rollback();
			LOGGER.error("find by example failed", e.getCause());
			return null;
		}
	}

	// ***************** Manually created methods ******************
	@SuppressWarnings("unchecked")
	public List<TaskAttachment> getTaskAttachmentByInstructorAndState(
			User user, String state) {
		LOGGER.info("finding list of TaskAttachments by Instructor and State");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<TaskAttachment> attachmentList = null;
		try {
			transaction = session.beginTransaction();
			attachmentList = session
					.getNamedQuery(GET_TASK_ATTACHMENT_BY_INSTRUCTOR_ID_AND_STATE)
					.setString(STATE, state)
					.setInteger(INSTRUCTOR_ID, user.getUserId()).list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return attachmentList;
	}

	@SuppressWarnings("unchecked")
	public List<TaskAttachment> getTaskAttachmentByStudentAndTask(User student,
			Task task) {
		LOGGER.info("finding list of TaskAttachments by Student and Task");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<TaskAttachment> attachmentList = null;
		try {
			transaction = session.beginTransaction();
			attachmentList = session
					.getNamedQuery(GET_TASK_ATTACHMENT_BY_STUDENT_ID_AND_TASK_ID)
					.setInteger(TASK_ID, task.getTaskId())
					.setInteger(STUDENT_ID, student.getUserId()).list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return attachmentList;
	}
	
	public String getFilePathOfTaskAttachmentByTaskAndState(Task task, String state) {
		LOGGER.info("finding FilePath of TaskAttachment by Task and State");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		String filePath = null;
		try {
			transaction = session.beginTransaction();
			filePath = (String) session
					.getNamedQuery(GET_TASK_ATTACHMENT_NAME_BY_TASK_ID_AND_STATE)
					.setString(STATE, state)
					.setInteger(TASK_ID, task.getTaskId()).list().get(0);
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return filePath;
	}

	@SuppressWarnings("unchecked")
	public List<TaskAttachment> getTaskAttachmentByStudentTaskAndState(
			User student, Task task) {
		LOGGER.info("finding list of TaskAttachments by Student,Task and state");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<TaskAttachment> attachmentList = null;
		try {
			transaction = session.beginTransaction();
			attachmentList = session
					.getNamedQuery(GET_TASK_ATTACHMENT_BY_STUDENT_ID_TASK_ID_AND_STATE)
					.setInteger(TASK_ID, task.getTaskId())
					.setInteger(STUDENT_ID, student.getUserId())
					.setString(STATE, "literature").list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return attachmentList;
	}

}
